package question3;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import question3.tp_pile.PilePleineException;
import question3.tp_pile.PileVideException;

/**
 * Decrivez votre classe Controleur ici.
 * 
 * @author (votre nom) 
 * @version (un numero de version ou une date)
 */
public class Controleur extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton push, add, sub, mul, div, clear;
	private PileModele<Integer> pile;
	private JTextField donnee;

	public Controleur( PileModele<Integer> pile )
	{
		super();
		this.pile= pile;
		this.donnee= new JTextField( 8 );

		this.push= new JButton( "push" );
		this.add= new JButton( "+" );
		this.sub= new JButton( "-" );
		this.mul= new JButton( "*" );
		this.div= new JButton( "/" );
		this.clear= new JButton( "[]" );

		setLayout( new GridLayout( 2, 1 ) );
		add( donnee );
		donnee.addActionListener(new PushButton());
		JPanel boutons= new JPanel();
		boutons.setLayout( new FlowLayout() );
		boutons.add( push );
		push.addActionListener(new PushButton());
		boutons.add( add );
		add.addActionListener(new PlusButton());
		boutons.add( sub );
		sub.addActionListener(new MinusButton());
		boutons.add( mul );
		mul.addActionListener(new MultiButton());
		boutons.add( div );
		div.addActionListener(new DivButton());
		boutons.add( clear );
		clear.addActionListener(new ClearButton());
		add( boutons );
		boutons.setBackground( Color.red );
		actualiserInterface();
	} // Controleur()

	public void actualiserInterface()
	{

		if (pile.estVide())
			clear.setEnabled(false);
		else if(pile.taille() == 1)
			clear.setEnabled(true);
		if(pile.estPleine()){
			push.setEnabled(false);
		}
		else if(pile.taille() < 2){
			mul.setEnabled(false);
			div.setEnabled(false);
			add.setEnabled(false);
			sub.setEnabled(false);
		}
		else{
			push.setEnabled(true);
			mul.setEnabled(true);
			div.setEnabled(true);
			add.setEnabled(true);
			sub.setEnabled(true);
		}
	} // actualiserInterface()

	private Integer operande() throws NumberFormatException
	{
		return Integer.parseInt( donnee.getText() );
	} // operande()

	// a completer
	// en cas d'exception comme division par zero, mauvais format de nombre
	// la pile doit rester en l'etat (intacte)

	protected class PlusButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			try {
				pile.empiler(pile.depiler() + pile.depiler());
			} catch (PilePleineException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PileVideException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			actualiserInterface();
		}

	}

	protected class MinusButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			try {
				int a = pile.depiler();
				int b = pile.depiler();
				pile.empiler(b-a);
			} catch (PilePleineException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PileVideException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			actualiserInterface();
		}
	}

	protected class MultiButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			try {
				pile.empiler(pile.depiler() * pile.depiler());
			} catch (PilePleineException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PileVideException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			actualiserInterface();
		}

	}

	protected class DivButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			try {
				int a = pile.depiler();
				int b = pile.depiler();
				if(a == 0) {
					pile.empiler(b); 
					pile.empiler(a);
				}
				else pile.empiler(b/a);
			} catch (PilePleineException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PileVideException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			actualiserInterface();
		}

	}

	protected class PushButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
					pile.empiler(operande());
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			} catch (PilePleineException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			actualiserInterface();
		}

	}

	protected class ClearButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			try {
				while(!pile.estVide())
					pile.depiler();
			} catch (PileVideException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			actualiserInterface();
		}

	}
} // Controleur
